<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Happy dong
 * Created Time: 2019-11-23 10:14
 *
 * Version: 2.0
 */

namespace App\Modules\Coupon\Controllers;


use App\Exceptions\AllValidatorException;
use App\Lib\Facades\FLAuth;
use App\Modules\Coupon\Services\CouponService;
use App\Modules\Coupon\Transformers\GetStoreCouponListTransformer;
use App\Modules\Coupon\Validators\CouponMemberControllerValidator;
use Flower\Core\Http\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;

class CouponMemberController extends BaseController
{
	protected $couponService;
	public function __construct(CouponService $couponService)
	{
		$this->couponService = $couponService;
	}


	public function getRules()
	{
		return CouponMemberControllerValidator::getRules();
	}


	/**
	 * 获取店铺的优惠券列表
	 * @param Request $request
	 * @return mixed
	 */
	public function getStoreCouponList(Request $request){
		$list = $this->couponService->getStoreCouponList(FLAuth::id(),$request->store_id);
		$result = GetStoreCouponListTransformer::transform($list);
		Log::info('return_coupon_list',$result);
		return $this->handleApiResult($result);
	}

	/**
	 * 抢优惠券
	 * @param Request $request
	 * @return mixed
	 * @throws \App\Exceptions\NotFoundException
	 */
	public function pickUpCoupon(Request $request){
		$send_id = $request->id;
		$source = $request->source;
		if(empty($source) && empty($send_id)){
			throw new AllValidatorException(['msg'=>'直播的抢优惠券，id必须传切为数字']);
		}
		$result = $this->couponService->addCouponList($request,FLAuth::id());
		return $this->handleApiResult($result);
	}


	/**
	 * 用户领取的优惠券列表
	 * @param Request $request
	 * @return mixed
	 */
	public function getMemberCouponList(Request $request){
		$list = $this->couponService->getMemberCouponList(FLAuth::id(),$request->status);
		return $this->handleApiResult($list);
	}


	/**
	 * 直播间获取理发师发送的优惠券列表
	 * @return mixed
	 */
	public function getCouponList(Request $request){
		$user_id = $request->input('user_id');
		$store_id = $request->input('store_id');
		$result = $this->couponService->getCouponSendList($user_id,$store_id);
		return $this->handleApiResult($result);
	}

	/**
	 * 小程序检测理发师是否发布优惠券
	 * @param Request $request
	 * @return mixed
	 */
	public function checkHasCouponOut(Request $request){
		$user_id = $request->input('user_id');
		$store_id = $request->input('store_id');
		$member_id = FLAuth::id();
		$result = $this->couponService->checkHasCouponOut($user_id,$store_id,$member_id);
		return $this->handleApiResult($result);
	}


//	public function getCurrentCouponInLiveRoom(Request $request){
//		$user_id = $request->input('user_id');
//		$store_id = $request->input('store_id');
//		$result = $this->couponService->getCurrentCouponInLiveRoom($user_id,$store_id);
//		return $this->handleApiResult($result);
//	}
}
